﻿/// <reference path="../Turbo/Turbo.js" />
/// <reference path="../Turbo/ServerEventSender.js" />
/// <reference path="../Data/Data.js" />
/// <reference path="../Data/Template.js" />
/// <reference path="Group.js" />

var ____a;

var $GroupedRepeaterGrouping = TA.Turbo.Controls.GroupedRepeaterGrouping = function GroupedRepeaterGrouping()
{
	this.ServerEventSender();

	this._fields = new Array();
	this._hiddenGroups = new Array();
	this._groups = new Array();
}
$GroupedRepeaterGrouping.Inherit($ServerEventSender);
$GroupedRepeaterGrouping.Inherit(BaseJsonSerializable);
$GroupedRepeaterGrouping.Inherit(BaseJsonDeserializable);

if (____a)
{
	$GroupedRepeaterGrouping.prototype = new $ServerEventSender();
	$GroupedRepeaterGrouping.prototype.GroupedRepeaterGrouping = new $ServerEventSender();

	$GroupedRepeaterGrouping.prototype._groupTemplate = null;
	$GroupedRepeaterGrouping.prototype._fields = new Array();
	$GroupedRepeaterGrouping.prototype._getItemsContainer = null;
	$GroupedRepeaterGrouping.prototype._hiddenGroups = new Array();
	$GroupedRepeaterGrouping.prototype._groups = new Array();
}

$GroupedRepeaterGrouping.prototype._enabled = true;
$GroupedRepeaterGrouping.prototype._order = 0;



$GroupedRepeaterGrouping.prototype.get_Order = function get_Order()
{
	return this._order;
}
$GroupedRepeaterGrouping.prototype.get_Enabled = function get_Enabled()
{
	return this._enabled;
}
$GroupedRepeaterGrouping.prototype.set_Enabled = function set_Enabled(value)
{
	this._enabled = value;
}
$GroupedRepeaterGrouping.prototype.get_GroupTemplate = function get_GroupTemplate()
{
	return this._groupTemplate;
	return new $MarkupTemplate();
}
$GroupedRepeaterGrouping.prototype.get_HiddenGroups = function get_HiddenGroups()
{
	return this._hiddenGroups;
}
$GroupedRepeaterGrouping.prototype.get_Groups = function get_Groups()
{
	return this._groups;
}


$GroupedRepeaterGrouping.prototype.get_IsDirty = function get_IsDirty()
{
	return true;
}

$GroupedRepeaterGrouping.prototype.SerializeContents = function SerializeContents(jw)
{
	if (!this._enabled)
	{
		jw.Property("E", this._enabled);
	}
}

$GroupedRepeaterGrouping.prototype.DeserializeContent = function DeserializeContent(propShortName, value)
{
	switch (propShortName)
	{
		case "E":
			this._enabled = value; break;
		case "F":
			this._fields = value; break;
		case "O":
			this._order = value; break;
		case "T":
			this._groupTemplate = new $MarkupTemplate();
			this._groupTemplate.Deserialize(value);
			break;
		case "P":
			if (value != null && value != "")
				this.AddEvent("ParseRenderedGroups", value);
			break;
		case "C":
			if (value != null && value != "")
				this.AddEvent("Compare", value);
			break;
		case "B":
			if (value != null && value != "")
				this.AddEvent("Bind", value);
			break;
	}
}
$GroupedRepeaterGrouping.prototype.GetElements = function GetElements(containerElement)
{
	var args = {
		ContainerElement: containerElement,
		Groups: new Array()
	};
	this.RaiseEvent("ParseRenderedGroups", args);

	return args.Groups;
}
$GroupedRepeaterGrouping.prototype.GetItemsContainer = function GetItemsContainer(elements)
{
	var res = null;
	for (var iElem = 0; iElem < elements.length; ++iElem)
	{
		elements[iElem].PerformRecursive(function(elem)
		{
			if (elem.getAttribute("ItemsContainer") == "1")
			{
				res = elem;
				return true;
			}
		});
		if (res != null)
			break;
	}
	return res;
}
$GroupedRepeaterGrouping.prototype.EntriesEqual = function EntriesEqual(x, y)
{
	if (this.IsHandled("Compare"))
	{
		var args = { X: x, Y: y, Result: 0 };
		this.RaiseEvent("Compare", args);
		return args.Result == 0;
	}

	for (var iField = 0; iField < this._fields.length; ++iField)
	{
		var field = this._fields[iField];

		if (TA.Turbo.Data.GetValue(x, field) != TA.Turbo.Data.GetValue(y, field))
			return false;
	}
	return true;
}
$GroupedRepeaterGrouping.prototype.LeaseGroup = function LeaseGroup(repeater, groupContainer)
{
	if (____a) groupContainer = new $IRepeaterGroupContainer();

	// first scan unused groups; these are already in place (no need for DOM manipulation)
	var myself = this;
	var group = groupContainer.get_UnusedGroups().First(function(k) { return k._grouping == myself; });
	if (group != null)
	{
		groupContainer.get_UnusedGroups().Remove(group);
	}
	else
	{
		// then scan all groups; maybe there are unused groups elsewhere (which we can move by DOM manipulation)
		if (this.get_HiddenGroups().length > 0)
		{
			group = this.get_HiddenGroups().pop();
		}

		if (group == null)
		{
			// nothing. create new one
			group = this.CreateGroup(repeater);
		}

		group.Show(groupContainer.get_ContainerElement());
	}
	// in any case, add it to the groupContainer's groups collection
	groupContainer.get_Groups().push(group);
	return group;
}
$GroupedRepeaterGrouping.prototype.CreateGroup = function CreateGroup(repeater)
{
	var group = new $RepeaterGroup(this, this.get_GroupTemplate()._name + TA.Turbo.Common.ClientIdSeparator + this._groups.length.toString(), repeater.get_Control(), null);
	group.Init();
	this._groups.push(group); // for ID purposes (to maintain count of all)
	repeater.get_GroupContainers().push(group);
	return group;
}































var $RepeaterGroupingCollection = TA.Turbo.Common.RepeaterGroupingCollection = function RepeaterGroupingCollection()
{
	this.JsonCompatibleCollection();
}
$RepeaterGroupingCollection.Inherit(JsonCompatibleCollection);
if (_____a)
{
	$RepeaterGroupingCollection.prototype = new JsonCompatibleCollection();
}

$RepeaterGroupingCollection.prototype.CreateOneItem = function CreateOneItem()
{
	return new $GroupedRepeaterGrouping();
}
